<?php

namespace CZan\Utils;

use Exception;

class Path
{

    /**
     * 上传路径转化,默认路径
     * @param string $path
     * @param int $type
     * @param bool $force
     * @return string
     * @throws Exception
     */
    public static function make_path(string $path, int $type = 2, bool $force = false): string
    {
        $path = DIRECTORY_SEPARATOR . ltrim(rtrim($path));
        switch ($type) {
            case 1:
                $path .= DIRECTORY_SEPARATOR . date('Y');
                break;
            case 2:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m');
                break;
            case 3:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
                break;
        }
        try {
            if (is_dir($path) == true || mkdir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . $path, 0777, true) == true) {
                return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
            } else return '';
        } catch (Exception $e) {
            if ($force)
                throw new Exception($e->getMessage());
            return '无法创建文件夹，请检查您的上传目录权限：' . $path . DIRECTORY_SEPARATOR . 'attach' . DIRECTORY_SEPARATOR;
        }
    }

    /**
     * 设置附加路径
     * @param string $image
     * @param string $siteUrl
     * @return string
     */
    public static function set_file_url(string $image, string $siteUrl): string
    {
        $domainTop = substr($image, 0, 4);
        if ($domainTop == 'http') return $image;
        $image = str_replace('\\', '/', $image);
        return $siteUrl . $image;
    }

    /**
     * url转换路径
     * @param string $rootPath
     * @param string $url
     * @return string
     */
    public static function url_to_path(string $rootPath, string $url): string
    {
        $path = trim(str_replace('/', DIRECTORY_SEPARATOR, $url), DIRECTORY_SEPARATOR);
        if (0 !== strripos($path, 'public'))
            $path = 'public' . DIRECTORY_SEPARATOR . $path;
        return $rootPath . $path;
    }

    /**
     * 路径转url路径
     * @param $path
     * @return string
     */
    public static function path_to_url($path): string
    {
        return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
    }

    /**
     * CURL 检测远程文件是否在
     * @param $url
     * @return bool
     */
    public static function curl_file_exist($url): bool
    {
        $ch = curl_init();
        try {
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            $contents = curl_exec($ch);
            if (preg_match("/404/", $contents)) return false;
            if (preg_match("/403/", $contents)) return false;
            return true;
        } catch (Exception $e) {
            return false;
        }
    }
}
